package wnhz.com.business.groupshop.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.BeginActivity;
import wnhz.com.business.CompleteInfoActivity;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.groupshop.bean.PersonInfoBean;
import wnhz.com.business.groupshop.ui.CreditNumActivity;
import wnhz.com.business.groupshop.ui.MyAttentionActivity;
import wnhz.com.business.groupshop.ui.MyCollectActivity;
import wnhz.com.business.groupshop.ui.TuiKuanActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.groupshop.ui.AllOrderActivity;
import wnhz.com.business.groupshop.ui.FundActivity;
import wnhz.com.business.groupshop.ui.HelpCenterActivity;
import wnhz.com.business.groupshop.ui.MakeMerchatActivity;
import wnhz.com.business.groupshop.ui.MeassageChangeActivity;
import wnhz.com.business.groupshop.ui.MessageActivity;
import wnhz.com.business.groupshop.ui.SettingActivity;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.netcallback.StringDialogCallback;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.view.CircleImageView;

public class HomeFragment5 extends BaseFragment implements View.OnClickListener, ActionBarClickListener {

    @BindView(R.id.iv_head)
    CircleImageView iv_head;
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.ll_allOrder)
    LinearLayout ll_allOrder;
    @BindView(R.id.ll_setting)
    LinearLayout ll_setting;
    @BindView(R.id.ll_help)
    LinearLayout ll_help;
    @BindView(R.id.ll_main)
    LinearLayout ll_main;
    @BindView(R.id.ll_fund)
    LinearLayout ll_fund;
    @BindView(R.id.ll_attention)
    LinearLayout ll_attention;
    @BindView(R.id.ll_collect)
    LinearLayout ll_collect;
    @BindView(R.id.ll_one)
    LinearLayout ll_one;
    @BindView(R.id.ll_two)
    LinearLayout ll_two;
    @BindView(R.id.ll_three)
    LinearLayout ll_three;
    @BindView(R.id.ll_four)
    LinearLayout ll_four;
    @BindView(R.id.ll_fifth)
    LinearLayout ll_fifth;
    @BindView(R.id.ll_ticket)
    LinearLayout ll_ticket;
    @BindView(R.id.ll_userFaith)
    LinearLayout ll_userFaith;
    @BindView(R.id.ll_shenqin)
    LinearLayout ll_shenqin;
    @BindView(R.id.tv_userName)
    TextView tv_userName;
    @BindView(R.id.ll_refund)
    LinearLayout ll_refund;
    @BindView(R.id.ll_xingyongnum)
    LinearLayout ll_xingyongnum;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.ll_message)
    LinearLayout llMessage;
    @BindView(R.id.credit_value)
    TextView credit_value;
    @BindView(R.id.tv_msgnum)
    TextView tv_msgnum;

    private BaseActivity activity;
    private View view;
    private PersonInfoBean personInfoBean = new PersonInfoBean();
    public static HomeFragment5 newInstance() {
        HomeFragment5 homeFragment5 = new HomeFragment5();
        return homeFragment5;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtils.e("wxf","免费商城的个人中心");
        view = inflater.inflate(R.layout.fragment_home_fragment5, container, false);
        ButterKnife.bind(this, view);
        activity = (BaseActivity) getActivity();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        initView();
        loadData();
        return view;
    }

    /**
     * 获取个人信息
     */
    private void loadData() {

        Map<String,String> map = new HashMap<>();
        if (MyApplication.getInstance().getInfoBean() == null){
            MyApplication.getInstance().BackLoginActivity();
            return;
        }else{
            map.put("token",MyApplication.getInstance().getInfoBean().getToken());
        }
        OkGo.<PersonInfoBean>post(Url.UCENTER_INFO)
                .tag(this)
                .params(map,false)
                .execute(new DialogCallback<PersonInfoBean>(activity) {
                    @Override
                    public void onSuccess(Response<PersonInfoBean> response) {
                        if (response.body().getRe().equals("1")){
                            Log.e("获取个人信息",""+response.body());
                            personInfoBean = response.body();
                            Glide.with(activity).load(personInfoBean.getInfo().getHead_img()).into(iv_head);
                            tv_userName.setText(personInfoBean.getInfo().getUsername());
                            phone.setText(personInfoBean.getInfo().getMobile());
                            credit_value.setText(Base64Util.decodedString2(personInfoBean.getInfo().getBalance()));
//                            tv_msgnum.setText(personInfoBean.getInfo().);
                        }else {
                            ToastUtils.showToast(activity,""+response.body().getInfo());
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<PersonInfoBean> response) {
                        super.onError(response);
                    }
                });
    }

    private void initView() {
        iv_head.setOnClickListener(this);
        tv_userName.setOnClickListener(this);
        tv_phone.setOnClickListener(this);
        ll_collect.setOnClickListener(this);
        ll_attention.setOnClickListener(this);
        ll_fund.setOnClickListener(this);
        ll_userFaith.setOnClickListener(this);
        ll_ticket.setOnClickListener(this);
        ll_refund.setOnClickListener(this);
        ll_shenqin.setOnClickListener(this);
        ll_help.setOnClickListener(this);
        ll_main.setOnClickListener(this);
        ll_allOrder.setOnClickListener(this);
        ll_one.setOnClickListener(this);
        ll_two.setOnClickListener(this);
        ll_three.setOnClickListener(this);
        ll_four.setOnClickListener(this);
        ll_fifth.setOnClickListener(this);
        ll_setting.setOnClickListener(this);
        ll_xingyongnum.setOnClickListener(this);
        llMessage.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {

            //头像
            case R.id.iv_head:
                intent = new Intent(activity, CompleteInfoActivity.class);
                intent.putExtra("completetype","2");//从个人中心界面进入
                startActivity(intent);
//                startActivity(MeassageChangeActivity.createIntent(activity));
                break;
            //姓名
            case R.id.tv_userName:
                startActivity(MeassageChangeActivity.createIntent(activity));
                break;
            //手机号
            case R.id.tv_phone:
                startActivity(MeassageChangeActivity.createIntent(activity));
                break;
            //标题信用值
            case R.id.ll_xingyongnum:
                startActivity(CreditNumActivity.createIntent(activity));
                break;
            //收藏
            case R.id.ll_collect:
                startActivity(MyCollectActivity.createIntent(activity));

                break;
            //消息
            case R.id.ll_message:
                startActivity(MessageActivity.createIntent(activity));

                break;
            //关注
            case R.id.ll_attention:
                startActivity(MyAttentionActivity.createIntent(activity));
                break;
            //钱包
            case R.id.ll_fund:
                startActivity(FundActivity.createIntent(activity));
                break;
            //免单券
            case R.id.ll_ticket:

                break;
            //信用值
            case R.id.ll_userFaith:
                startActivity(CreditNumActivity.createIntent(activity));
                break;
            //退款
            case R.id.ll_refund:
                startActivity(TuiKuanActivity.createIntent(activity));
                break;
            //申请成为商家
            case R.id.ll_shenqin:
                startActivity(MakeMerchatActivity.createIntent(activity));
                break;
            //帮助中心
            case R.id.ll_help:
                startActivity(HelpCenterActivity.createIntent(activity, 2));//int   type   1-规则  2-帮助中心  3-信用秘籍
                break;
            //返回主界面
            case R.id.ll_main:
                startActivity(BeginActivity.createIntent(activity));
                activity.finish();
                break;
            case R.id.ll_allOrder:   //所有订单
                startActivity(AllOrderActivity.createIntent(activity, "0"));
                break;
            case R.id.ll_one:    //单个状态订单
                startActivity(AllOrderActivity.createIntent(activity, "1"));
                break;
            case R.id.ll_two:
                startActivity(AllOrderActivity.createIntent(activity, "3"));
                break;
            case R.id.ll_three:
                startActivity(AllOrderActivity.createIntent(activity, "4"));
                break;
            case R.id.ll_four:
                startActivity(AllOrderActivity.createIntent(activity, "2"));
                break;
            case R.id.ll_fifth:
                startActivity(AllOrderActivity.createIntent(activity, "5"));
                break;
            case R.id.ll_setting:
                startActivity(SettingActivity.createIntent(activity));
                break;
            default:
                break;
        }
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
